home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / emacs / emacs18.57 / 1857sr~1.zoo / source / term.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-02  |  32.8 KB  |  1,256 lines

  1. /* terminal control module for terminals described by TERMCAP
  2.    Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include <stdio.h>
  22. #include <ctype.h>
  23. #include "config.h"
  24. #include "termhooks.h"
  25. #include "termchar.h"
  26. #include "termopts.h"
  27. #include "cm.h"
  28.  
  29. /**
  30.  **  (sjk)++ stuff for the xconsole i/o routines.
  31.  **/
  32. #if defined(atarist)
  33. #include <keycodes.h>
  34. extern void initialize_st_bindings();
  35. #endif 
  36.  
  37. #define max(a, b) ((a) > (b) ? (a) : (b))
  38. #define min(a, b) ((a) < (b) ? (a) : (b))
  39.  
  40. #define OUTPUT(a) tputs (a, screen_height - curY, cmputc)
  41. #define OUTPUT1(a) tputs (a, 1, cmputc)
  42. #define OUTPUTL(a, lines) tputs (a, lines, cmputc)
  43. #define OUTPUT_IF(a) { if (a) tputs (a, screen_height - curY, cmputc); }
  44. #define OUTPUT1_IF(a) { if (a) tputs (a, 1, cmputc); }
  45.  
  46. /* Terminal charateristics that higher levels want to look at.
  47.    These are all extern'd in termchar.h */
  48.  
  49. int screen_width;        /* Number of usable columns */
  50. int screen_height;        /* Number of lines */
  51. int must_write_spaces;        /* Nonzero means spaces in the text
  52.                    must actually be output; can't just skip
  53.                    over some columns to leave them blank.  */
  54. int min_padding_speed;        /* Speed below which no padding necessary */
  55.  
  56. int line_ins_del_ok;        /* Terminal can insert and delete lines */
  57. int char_ins_del_ok;        /* Terminal can insert and delete chars */
  58. int scroll_region_ok;        /* Terminal supports setting the scroll window */
  59. int memory_below_screen;    /* Terminal remembers lines scrolled off bottom */
  60. int fast_clear_end_of_line;    /* Terminal has a `ce' string */
  61.  
  62. int dont_calculate_costs;    /* Nonzero means don't bother computing */
  63.                 /* various cost tables; we won't use them.  */
  64.  
  65. /* Nonzero means no need to redraw the entire screen on resuming
  66.    a suspended Emacs.  This is useful on terminals with multiple pages,
  67.    where one page is used for Emacs and another for all else. */
  68. int no_redraw_on_reenter;
  69.  
  70. /* DCICcost[n] is cost of inserting N characters.
  71.    DCICcost[-n] is cost of deleting N characters. */
  72.  
  73. #define DCICcost (&DC_ICcost[screen_width])
  74. int *DC_ICcost;
  75.  
  76. /* Hook functions that you can set to snap out the functions in this file.
  77.    These are all extern'd in termhooks.h  */
  78.  
  79. int (*move_cursor_hook) ();
  80. int (*raw_move_cursor_hook) ();
  81.  
  82. int (*clear_to_end_hook) ();
  83. int (*clear_screen_hook) ();
  84. int (*clear_end_of_line_hook) ();
  85.  
  86. int (*ins_del_lines_hook) ();
  87.  
  88. int (*change_line_highlight_hook) ();
  89. int (*reassert_line_highlight_hook) ();
  90.  
  91. int (*insert_chars_hook) ();
  92. int (*output_chars_hook) ();
  93. int (*delete_chars_hook) ();
  94.  
  95. int (*ring_bell_hook) ();
  96.  
  97. int (*reset_terminal_modes_hook) ();
  98. int (*set_terminal_modes_hook) ();
  99. int (*update_begin_hook) ();
  100. int (*update_end_hook) ();
  101. int (*set_terminal_window_hook) ();
  102.  
  103. int (*read_socket_hook) ();
  104. int (*fix_screen_hook) ();
  105. int (*calculate_costs_hook) ();
  106.  
  107. /* Strings, numbers and flags taken from the termcap entry.  */
  108.  
  109. char *TS_ins_line;        /* termcap "al" */
  110. char *TS_ins_multi_lines;    /* "AL" (one parameter, # lines to insert) */
  111. char *TS_bell;            /* "bl" */
  112. char *TS_clr_to_bottom;        /* "cd" */
  113. char *TS_clr_line;        /* "ce", clear to end of line */
  114. char *TS_clr_screen;        /* "cl" */
  115. char *TS_set_scroll_region;    /* "cs" (2 params, first line and last line) */
  116. char *TS_set_scroll_region_1;   /* "cS" (4 params: total lines,
  117.                    lines above scroll region, lines below it,
  118.                    total lines again) */
  119. char *TS_del_char;        /* "dc" */
  120. char *TS_del_multi_chars;    /* "DC" (one parameter, # chars to delete) */
  121. char *TS_del_line;        /* "dl" */
  122. char *TS_del_multi_lines;    /* "DL" (one parameter, # lines to delete) */
  123. char *TS_delete_mode;        /* "dm", enter character-delete mode */
  124. char *TS_end_delete_mode;    /* "ed", leave character-delete mode */
  125. char *TS_end_insert_mode;    /* "ei", leave character-insert mode */
  126. char *TS_ins_char;        /* "ic" */
  127. char *TS_ins_multi_chars;    /* "IC" (one parameter, # chars to insert) */
  128. char *TS_insert_mode;        /* "im", enter character-insert mode */
  129. char *TS_pad_inserted_char;    /* "ip".  Just padding, no commands.  */
  130. char *TS_end_keypad_mode;    /* "ke" */
  131. char *TS_keypad_mode;        /* "ks" */
  132. char *TS_pad_char;        /* "pc", char to use as padding */
  133. char *TS_repeat;        /* "rp" (2 params, # times to repeat
  134.                    and character to be repeated) */
  135. char *TS_end_standout_mode;    /* "se" */
  136. char *TS_fwd_scroll;        /* "sf" */
  137. char *TS_standout_mode;        /* "so" */
  138. char *TS_rev_scroll;        /* "sr" */
  139. char *TS_end_termcap_modes;    /* "te" */
  140. char *TS_termcap_modes;        /* "ti" */
  141. char *TS_visible_bell;        /* "vb" */
  142. char *TS_end_visual_mode;    /* "ve" */
  143. char *TS_visual_mode;        /* "vi" */
  144. char *TS_set_window;        /* "wi" (4 params, start and end of window,
  145.                    each as vpos and hpos) */
  146.  
  147. int TF_hazeltine;    /* termcap hz flag. */
  148. int TF_insmode_motion;    /* termcap mi flag: can move while in insert mode. */
  149. int TF_standout_motion;    /* termcap mi flag: can move while in standout mode. */
  150. int TF_underscore;    /* termcap ul flag: _ underlines if overstruck on
  151.                nonblank position.  Must clear before writing _.  */
  152. int TF_teleray;        /* termcap xt flag: many weird consequences.  For t1061. */
  153.  
  154. int TF_xs;        /* Nonzero for "xs".  If set together with
  155.                TN_standout_width == 0, it means don't bother
  156.                to write any end-standout cookies.  */
  157.  
  158. int TN_standout_width;    /* termcap sg number: width occupied by standout markers */
  159.  
  160. static int RPov;    /* # chars to start a TS_repeat */
  161.  
  162. static int delete_in_insert_mode;    /* delete mode == insert mode */
  163.  
  164. static int se_is_so;    /* 1 if same string both enters and leaves standout mode */
  165.  
  166. /* internal state */
  167.  
  168. /* Number of chars of space used for standout marker at beginning of line,
  169.    or'd with 0100.  Zero if no standout marker at all.  */
  170. /* used iff TN_standout_width >= 0. */
  171. char *chars_wasted;
  172. static char *copybuf;
  173.  
  174. /* nonzero means supposed to write text in standout mode.  */
  175. int standout_requested;
  176.  
  177. int insert_mode;            /* Nonzero when in insert mode.  */
  178. int standout_mode;            /* Nonzero when in standout mode.  */
  179.  
  180. /* Size of window specified by higher levels.
  181.    This is the number of lines, starting from top of screen,
  182.    to participate in ins/del line operations.
  183.    Effectively it excludes the bottom
  184.       screen_height - specified_window_size
  185.    lines from those operations.  */
  186.  
  187. int specified_window;
  188.  
  189. char *tparam ();
  190.  
  191. ring_bell ()
  192. {
  193.   if (ring_bell_hook)
  194.     {
  195.       (*ring_bell_hook) ();
  196.       return;
  197.     }
  198.   OUTPUT (TS_visible_bell && visible_bell ? TS_visible_bell : TS_bell);
  199. }
  200.  
  201. set_terminal_modes ()
  202. {
  203.   if (set_terminal_modes_hook)
  204.     {
  205.       (*set_terminal_modes_hook) ();
  206.       return;
  207.     }
  208.   OUTPUT_IF (TS_termcap_modes);
  209.   OUTPUT_IF (TS_visual_mode);
  210.   OUTPUT_IF (TS_keypad_mode);
  211.   losecursor ();
  212. }
  213.  
  214. reset_terminal_modes ()
  215. {
  216.   if (reset_terminal_modes_hook)
  217.     {
  218.       (*reset_terminal_modes_hook) ();
  219.       return;
  220.     }
  221.   if (TN_standout_width < 0)
  222.     turn_off_highlight ();
  223.   turn_off_insert ();
  224.   OUTPUT_IF (TS_end_keypad_mode);
  225.   OUTPUT_IF (TS_end_visual_mode);
  226.   OUTPUT_IF (TS_end_termcap_modes);
  227. }
  228.  
  229. update_begin ()
  230. {
  231.   if (update_begin_hook)
  232.     (*update_begin_hook) ();
  233. }
  234.  
  235. update_end ()
  236. {
  237.   if (update_end_hook)
  238.     {
  239.       (*update_end_hook) ();
  240.       return;
  241.     }
  242.   turn_off_insert ();
  243.   background_highlight ();
  244.   standout_requested = 0;
  245. }
  246.  
  247. set_terminal_window (size)
  248.      int size;
  249. {
  250.   if (set_terminal_window_hook)
  251.     {
  252.       (*set_terminal_window_hook) (size);
  253.       return;
  254.     }
  255.   specified_window = size ? size : screen_height;
  256.   if (!scroll_region_ok)
  257.     return;
  258.   set_scroll_region (0, specified_window);
  259. }
  260.  
  261. set_scroll_region (start, stop)
  262.      int start, stop;
  263. {
  264.   char *buf;
  265.   if (TS_set_scroll_region)
  266.     {
  267.       buf = tparam (TS_set_scroll_region, 0, 0, start, stop - 1);
  268.     }
  269.   else if (TS_set_scroll_region_1)
  270.     {
  271.       buf = tparam (TS_set_scroll_region_1, 0, 0,
  272.             screen_height, start, screen_height - stop, screen_height);
  273.     }
  274.   else
  275.     {
  276.       buf = tparam (TS_set_window, 0, 0, start, 0, stop, screen_width);
  277.     }
  278.   OUTPUT (buf);
  279.   free (buf);
  280.   losecursor ();
  281. }
  282.  
  283. turn_on_insert ()
  284. {
  285.   if (!insert_mode)
  286.     OUTPUT (TS_insert_mode);
  287.   insert_mode = 1;
  288. }
  289.  
  290. turn_off_insert ()
  291. {
  292.   if (insert_mode)
  293.     OUTPUT (TS_end_insert_mode);
  294.   insert_mode = 0;
  295. }
  296.  
  297. /* Handle highlighting when TN_standout_width (termcap sg) is not specified.
  298.    In these terminals, output is affected by the value of standout
  299.    mode when the output is written.
  300.  
  301.    These functions are called on all terminals, but do nothing
  302.    on terminals whose standout mode does not work that way.  */
  303.  
  304. turn_off_highlight ()
  305. {
  306.   if (TN_standout_width < 0)
  307.     {
  308.       if (standout_mode)
  309.     OUTPUT_IF (TS_end_standout_mode);
  310.       standout_mode = 0;
  311.     }
  312. }
  313.  
  314. turn_on_highlight ()
  315. {
  316.   if (TN_standout_width < 0)
  317.     {
  318.       if (!standout_mode)
  319.     OUTPUT_IF (TS_standout_mode);
  320.       standout_mode = 1;
  321.     }
  322. }
  323.  
  324. /* Set standout mode to the state it should be in for
  325.    empty space inside windows.  What this is,
  326.    depends on the user option inverse-video.  */
  327.  
  328. background_highlight ()
  329. {
  330.   if (TN_standout_width >= 0)
  331.     return;
  332.   if (inverse_video)
  333.     turn_on_highlight ();
  334.   else
  335.     turn_off_highlight ();
  336. }
  337.  
  338. /* Set standout mode to the mode specified for the text to be output.  */
  339.  
  340. static
  341. highlight_if_desired ()
  342. {
  343.   if (TN_standout_width >= 0)
  344.     return;
  345.   if (!inverse_video == !standout_requested)
  346.     turn_off_highlight ();
  347.   else
  348.     turn_on_highlight ();
  349. }
  350.  
  351. /* Handle standout mode for terminals in which TN_standout_width >= 0.
  352.    On these terminals, standout is controlled by markers that
  353.    live inside the screen memory.  TN_standout_width is the width
  354.    that the marker occupies in memory.  Standout runs from the marker
  355.    to the end of the line on some terminals, or to the next
  356.    turn-off-standout marker (TS_end_standout_mode) string
  357.    on other terminals.  */
  358.  
  359. /* Write a standout marker or end-standout marker at the front of the line
  360.    at vertical position vpos.  */
  361.  
  362. write_standout_marker (flag, vpos)
  363.      int flag, vpos;
  364. {
  365.   if (flag || (TS_end_standout_mode && !TF_teleray && !se_is_so
  366.            && !(TF_xs && TN_standout_width == 0)))
  367.     {
  368.       cmgoto (vpos, 0);
  369.       cmplus (TN_standout_width);
  370.       OUTPUT (flag ? TS_standout_mode : TS_end_standout_mode);
  371.       chars_wasted[curY] = TN_standout_width | 0100;
  372.     }
  373. }
  374.  
  375. /* External interface to control of standout mode.
  376.    Call this when about to modify line at position VPOS
  377.    and not change whether it is highlighted.  */
  378.  
  379. reassert_line_highlight (highlight, vpos)
  380.      int highlight;
  381.      int vpos;
  382. {
  383.   if (reassert_line_highlight_hook)
  384.     {
  385.       (*reassert_line_highlight_hook) (highlight, vpos);
  386.       return;
  387.     }
  388.   if (TN_standout_width < 0)
  389.     /* Handle terminals where standout takes affect at output time */
  390.     standout_requested = highlight;
  391.   else if (chars_wasted[vpos] == 0)
  392.     /* For terminals with standout markers, write one on this line
  393.        if there isn't one already.  */
  394.     write_standout_marker (highlight, vpos);
  395. }
  396.  
  397. /* Call this when about to modify line at position VPOS
  398.    and change whether it is highlighted.  */
  399.  
  400. change_line_highlight (new_highlight, vpos, first_unused_hpos)
  401.      int new_highlight, vpos, first_unused_hpos;
  402. {
  403.   standout_requested = new_highlight;
  404.   if (change_line_highlight_hook)
  405.     {
  406.       (*change_line_highlight_hook) (new_highlight, vpos, first_unused_hpos);
  407.       return;
  408.     }
  409.  
  410.   move_cursor (vpos, 0);
  411.  
  412.   if (TN_standout_width < 0)
  413.     background_highlight ();
  414.   /* If line starts with a marker, delete the marker */
  415.   else if (TS_clr_line && chars_wasted[curY])
  416.     {
  417.       turn_off_insert ();
  418.       /* On Teleray, make sure to erase the SO marker.  */
  419.       if (TF_teleray)
  420.     {
  421.       cmgoto (curY - 1, screen_width - 4);
  422.       OUTPUT ("\033S");
  423.       curY++;        /* ESC S moves to next line where the TS_standout_mode was */
  424.       curX = 0;
  425.     }
  426.       else
  427.     cmgoto (curY, 0);    /* reposition to kill standout marker */
  428.     }
  429.   clear_end_of_line_raw (first_unused_hpos);
  430.   reassert_line_highlight (new_highlight, curY);
  431. }
  432.  
  433. /* Move to absolute position, specified origin 0 */
  434.  
  435. move_cursor (row, col)
  436. {
  437.   col += chars_wasted[row] & 077;
  438.   if (move_cursor_hook)
  439.     {
  440.       (*move_cursor_hook) (row, col);
  441.       return;
  442.     }
  443.   if (curY == row && curX == col)
  444.     return;
  445.   if (!TF_standout_motion)
  446.     background_highlight ();
  447.   if (!TF_insmode_motion)
  448.     turn_off_insert ();
  449.   cmgoto (row, col);
  450. }
  451.  
  452. /* Similar but don't take any account of the wasted characters.  */
  453.  
  454. raw_move_cursor (row, col)
  455. {
  456.   if (raw_move_cursor_hook)
  457.     {
  458.       (*raw_move_cursor_hook) (row, col);
  459.       return;
  460.     }
  461.   if (curY == row && curX == col)
  462.     return;
  463.   if (!TF_standout_motion)
  464.     background_highlight ();
  465.   if (!TF_insmode_motion)
  466.     turn_off_insert ();
  467.   cmgoto (row, col);
  468. }
  469.  
  470. /* Erase operations */
  471.  
  472. /* clear from cursor to end of screen */
  473. clear_to_end ()
  474. {
  475.   register int i;
  476.  
  477.   if (clear_to_end_hook)
  478.     {
  479.       (*clear_to_end_hook) ();
  480.       return;
  481.     }
  482.   if (TS_clr_to_bottom)
  483.     {
  484.       background_highlight ();
  485.       OUTPUT (TS_clr_to_bottom);
  486.       bzero (chars_wasted + curY, screen_height - curY);
  487.     }
  488.   else
  489.     {
  490.       for (i = curY; i < screen_height; i++)
  491.     {
  492.       move_cursor (i, 0);
  493.       clear_end_of_line_raw (screen_width);
  494.     }
  495.     }
  496. }
  497.  
  498. /* Clear entire screen */
  499.  
  500. clear_screen ()
  501. {
  502.   if (clear_screen_hook)
  503.     {
  504.       (*clear_screen_hook) ();
  505.       return;
  506.     }
  507.   if (TS_clr_screen)
  508.     {
  509.       background_highlight ();
  510.       OUTPUT (TS_clr_screen);
  511.       bzero (chars_wasted, screen_height);
  512.       cmat (0, 0);
  513.     }
  514.   else
  515.     {
  516.       move_cursor (0, 0);
  517.       clear_to_end ();
  518.     }
  519. }
  520.  
  521. /* Clear to end of line, but do not clear any standout marker.
  522.    Assumes that the cursor is positioned at a character of real text,
  523.    which implies it cannot be before a standout marker
  524.    unless the marker has zero width.
  525.  
  526.    Note that the cursor may be moved.  */
  527.  
  528. clear_end_of_line (first_unused_hpos)
  529.      int first_unused_hpos;
  530. {
  531.   if (TN_standout_width == 0 && curX == 0 && chars_wasted[curY] != 0)
  532.     output_chars (" ", 1);
  533.   clear_end_of_line_raw (first_unused_hpos);
  534. }
  535.  
  536. /* Clear from cursor to end of line.
  537.    Assume that the line is already clear starting at column first_unused_hpos.
  538.    If the cursor is at a standout marker, erase the marker.
  539.  
  540.    Note that the cursor may be moved, on terminals lacking a `ce' string.  */
  541.  
  542. clear_end_of_line_raw (first_unused_hpos)
  543.      int first_unused_hpos;
  544. {
  545.   register int i;
  546.   first_unused_hpos += chars_wasted[curY] & 077;
  547.   if (clear_end_of_line_hook)
  548.     {
  549.       (*clear_end_of_line_hook) (first_unused_hpos);
  550.       return;
  551.     }
  552.   if (curX >= first_unused_hpos)
  553.     return;
  554.   /* Notice if we are erasing a magic cookie */
  555.   if (curX == 0)
  556.     chars_wasted[curY] = 0;
  557.   background_highlight ();
  558.   if (TS_clr_line)
  559.     {
  560.       OUTPUT1 (TS_clr_line);
  561.     }
  562.   else
  563.     {            /* have to do it the hard way */
  564.       turn_off_insert ();
  565.       for (i = curX; i < first_unused_hpos; i++)
  566.     {
  567.       if (termscript)
  568.         fputc (' ', termscript);
  569.       putchar (' ');
  570.     }
  571.       cmplus (first_unused_hpos - curX);
  572.     }
  573. }
  574.  
  575. output_chars (string, len)
  576.      register char *string;
  577.      int len;
  578. {
  579.   register char *p;
  580.   register int n;
  581.   register char *buf;
  582.   register int c;
  583.   char *first_check;
  584.  
  585.   if (output_chars_hook)
  586.     {
  587.       (*output_chars_hook) (string, len);
  588.       return;
  589.     }
  590.   highlight_if_desired ();
  591.   turn_off_insert ();
  592.  
  593.   /* Don't dare write in last column of bottom line, if AutoWrap,
  594.      since that would scroll the whole screen on some terminals.  */
  595.  
  596.   if (AutoWrap && curY + 1 == screen_height
  597.       && curX + len - (chars_wasted[curY] & 077) == screen_width)
  598.     len --;
  599.  
  600.   cmplus (len);
  601.  
  602.   first_check = string;
  603.  
  604.   if (RPov > len && !TF_underscore && !TF_hazeltine)
  605.     {
  606.       fwrite (string, 1, len, stdout);
  607.       if (ferror (stdout))
  608.     clearerr (stdout);
  609.       if (termscript)
  610.     fwrite (string, 1, len, termscript);
  611.     }
  612.   else
  613.     while (--len >= 0)
  614.       {
  615.     c = *string;
  616.     if (RPov + 1 < len && string >= first_check)
  617.       {
  618.         int repeat_count;
  619.  
  620.         p = string + 1;
  621.  
  622.         /* Now, len is number of chars left starting at p */
  623.         while (*p++ == c);
  624.         p--;
  625.  
  626.         repeat_count = p - string;
  627.         if (repeat_count > RPov)
  628.           {
  629.         buf = tparam (TS_repeat, 0, 0, *string, repeat_count);
  630.         tputs (buf, repeat_count, cmputc);
  631.         free (buf);
  632.         string = p;
  633.         len -= repeat_count - 1;
  634.         continue;
  635.           }
  636.         else
  637.           /* If all N identical chars are too few,
  638.          don't even consider the last N-1, the last N-2,...  */
  639.           first_check = p;
  640.       }
  641.     if (c == '_' && TF_underscore)
  642.       {
  643.         if (termscript)
  644.           fputc (' ', termscript);
  645.         putchar (' ');
  646.         OUTPUT (Left);
  647.       }
  648.     if (TF_hazeltine && c == '~')
  649.       c = '`';
  650.     if (termscript)
  651.       fputc (c, termscript);
  652.     putchar (c);
  653.     string++;
  654.       }
  655. }
  656.  
  657. /* If start is zero, insert blanks instead of a string at start */
  658.  
  659. insert_chars (start, len)
  660.      register char *start;
  661.      int len;
  662. {
  663.   register char *buf;
  664.   register int c;
  665.  
  666.   if (insert_chars_hook)
  667.     {
  668.       (*insert_chars_hook) (start, len);
  669.       return;
  670.     }
  671.   highlight_if_desired ();
  672.  
  673.   if (TS_ins_multi_chars)
  674.     {
  675.       buf = tparam (TS_ins_multi_chars, 0, 0, len);
  676.       OUTPUT1 (buf);
  677.       free (buf);
  678.       if (start)
  679.     output_chars (start, len);
  680.       return;
  681.     }
  682.  
  683.   turn_on_insert ();
  684.   cmplus (len);
  685.  
  686.   if (!TF_underscore && !TF_hazeltine && start
  687.       && TS_pad_inserted_char == 0 && TS_ins_char == 0)
  688.     {
  689.       fwrite (start, 1, len, stdout);
  690.       if (termscript)
  691.     fwrite (start, 1, len, termscript);
  692.     }
  693.   else
  694.     while (--len >= 0)
  695.       {
  696.     OUTPUT1_IF (TS_ins_char);
  697.     if (!start)
  698.       c = ' ';
  699.     else
  700.       {
  701.         c = *start++;
  702.         if (TF_hazeltine && c == '~')
  703.           c = '`';
  704.       }
  705.     if (termscript)
  706.       fputc (c, termscript);
  707.     putchar (c);
  708.     OUTPUT1_IF (TS_pad_inserted_char);
  709.     }
  710. }
  711.  
  712. delete_chars (n)
  713.      register int n;
  714. {
  715.   char *buf;
  716.   register int i;
  717.  
  718.   if (delete_chars_hook)
  719.     {
  720.       (*delete_chars_hook) (n);
  721.       return;
  722.     }
  723.  
  724.   if (delete_in_insert_mode)
  725.     {
  726.       turn_on_insert ();
  727.     }
  728.   else
  729.     {
  730.       turn_off_insert ();
  731.       OUTPUT_IF (TS_delete_mode);
  732.     }
  733.  
  734.   if (TS_del_multi_chars)
  735.     {
  736.       buf = tparam (TS_del_multi_chars, 0, 0, n);
  737.       OUTPUT1 (buf);
  738.       free (buf);
  739.     }
  740.   else
  741.     for (i = 0; i < n; i++)
  742.       OUTPUT1 (TS_del_char);
  743.   if (!delete_in_insert_mode)
  744.     OUTPUT_IF (TS_end_delete_mode);
  745. }
  746.  
  747. /* Insert N lines at vpos VPOS.  If N is negative, delete -N lines.  */
  748.  
  749. ins_del_lines (vpos, n)
  750.      int vpos, n;
  751. {
  752.   char *multi = n > 0 ? TS_ins_multi_lines : TS_del_multi_lines;
  753.   char *single = n > 0 ? TS_ins_line : TS_del_line;
  754.   char *scroll = n > 0 ? TS_rev_scroll : TS_fwd_scroll;
  755.  
  756.   register int i = n > 0 ? n : -n;
  757.   register char *buf;
  758.  
  759.   if (ins_del_lines_hook)
  760.     {
  761.       (*ins_del_lines_hook) (vpos, n);
  762.       return;
  763.     }
  764.  
  765.   /* If the lines below the insertion are being pushed
  766.      into the end of the window, this is the same as clearing;
  767.      and we know the lines are already clear, since the matching
  768.      deletion has already been done.  So can ignore this.  */
  769.   /* If the lines below the deletion are blank lines coming
  770.      out of the end of the window, don't bother,
  771.      as there will be a matching inslines later that will flush them. */
  772.   if (scroll_region_ok && vpos + i >= specified_window)
  773.     return;
  774.   if (!memory_below_screen && vpos + i >= screen_height)
  775.     return;
  776.  
  777.   if (multi)
  778.     {
  779.       raw_move_cursor (vpos, 0);
  780.       background_highlight ();
  781.       buf = tparam (multi, 0, 0, i);
  782.       OUTPUT (buf);
  783.       free (buf);
  784.     }
  785.   else if (single)
  786.     {
  787.       raw_move_cursor (vpos, 0);
  788.       background_highlight ();
  789.       while (--i >= 0)
  790.     OUTPUT (single);
  791.       if (TF_teleray)
  792.     curX = 0;
  793.     }
  794.   else
  795.     {
  796.       set_scroll_region (vpos, specified_window);
  797.       if (n < 0)
  798.     raw_move_cursor (specified_window - 1, 0);
  799.       else
  800.     raw_move_cursor (vpos, 0);
  801.       background_highlight ();
  802.       while (--i >= 0)
  803.     OUTPUTL (scroll, specified_window - vpos);
  804.       set_scroll_region (0, specified_window);
  805.     }
  806.  
  807.   if (TN_standout_width >= 0)
  808.     {
  809.       register lower_limit
  810.     = scroll_region_ok ? specified_window : screen_height;
  811.       if (n < 0)
  812.     {
  813.       bcopy (&chars_wasted[vpos - n], &chars_wasted[vpos],
  814.          lower_limit - vpos + n);
  815.       bzero (&chars_wasted[lower_limit + n], - n);
  816.     }
  817.       else
  818.     {
  819.       bcopy (&chars_wasted[vpos], ©buf[vpos], lower_limit - vpos - n);
  820.       bcopy (©buf[vpos], &chars_wasted[vpos + n],
  821.          lower_limit - vpos - n);
  822.       bzero (&chars_wasted[vpos], n);
  823.     }
  824.     }
  825.   if (!scroll_region_ok && memory_below_screen && n < 0)
  826.     {
  827.       move_cursor (screen_height + n, 0);
  828.       clear_to_end ();
  829.     }
  830. }
  831.  
  832. extern int cost;        /* In cm.c */
  833. extern evalcost ();
  834.  
  835. /* Compute cost of sending "str", in characters,
  836.    not counting any line-dependent padding.  */
  837. string_cost (str)
  838.      char *str;
  839. {
  840.   cost = 0;
  841.   if (str)
  842.     tputs (str, 0, evalcost);
  843.   return cost;
  844. }
  845.  
  846. /* Compute cost of sending "str", in characters,
  847.    counting any line-dependent padding at one line.  */
  848. string_cost_one_line (str)
  849.      char *str;
  850. {
  851.   cost = 0;
  852.   if (str)
  853.     tputs (str, 1, evalcost);
  854.   return cost;
  855. }
  856.  
  857. /* Compute per line amount of line-dependent padding,
  858.    in tenths of characters.  */
  859. per_line_cost (str)
  860.      register char *str;
  861. {
  862.   cost = 0;
  863.   if (str)
  864.     tputs (str, 0, evalcost);
  865.   cost = - cost;
  866.   if (str)
  867.     tputs (str, 10, evalcost);
  868.   return cost;
  869. }
  870.  
  871. /* ARGSUSED */
  872. calculate_ins_del_char_costs ()
  873. {
  874.   int ins_startup_cost, del_startup_cost;
  875.   int ins_cost_per_char, del_cost_per_char;
  876.   register int i;
  877.   register int *p;
  878.  
  879.   if (TS_ins_multi_chars)
  880.     {
  881.       ins_cost_per_char = 0;
  882.       ins_startup_cost = string_cost_one_line (TS_ins_multi_chars);
  883.     }
  884.   else if (TS_ins_char || TS_pad_inserted_char
  885.        || (TS_insert_mode && TS_end_insert_mode))
  886.     {
  887.       ins_startup_cost = (30 * (string_cost (TS_insert_mode) + string_cost (TS_end_insert_mode))) / 100;
  888.       ins_cost_per_char = (string_cost_one_line (TS_ins_char)
  889.                + string_cost_one_line (TS_pad_inserted_char));
  890.     }
  891.   else
  892.     {
  893.       ins_startup_cost = 9999;
  894.       ins_cost_per_char = 0;
  895.     }
  896.  
  897.   if (TS_del_multi_chars)
  898.     {
  899.       del_cost_per_char = 0;
  900.       del_startup_cost = string_cost_one_line (TS_del_multi_chars);
  901.     }
  902.   else if (TS_del_char)
  903.     {
  904.       del_startup_cost = (string_cost (TS_delete_mode)
  905.               + string_cost (TS_end_delete_mode));
  906.       if (delete_in_insert_mode)
  907.     del_startup_cost /= 2;
  908.       del_cost_per_char = string_cost_one_line (TS_del_char);
  909.     }
  910.   else
  911.     {
  912.       del_startup_cost = 9999;
  913.       del_cost_per_char = 0;
  914.     }
  915.  
  916.   /* Delete costs are at negative offsets */
  917.   p = &DCICcost[0];
  918.   for (i = screen_width; --i >= 0;)
  919.     *--p = (del_startup_cost += del_cost_per_char);
  920.  
  921.   /* Doing nothing is free */
  922.   p = &DCICcost[0];
  923.   *p++ = 0;
  924.  
  925.   /* Insert costs are at positive offsets */
  926.   for (i = screen_width; --i >= 0;)
  927.     *p++ = (ins_startup_cost += ins_cost_per_char);
  928. }
  929.  
  930. calculate_costs ()
  931. {
  932.   register char *s
  933.     = TS_set_scroll_region ? TS_set_scroll_region : TS_set_scroll_region_1;
  934.  
  935.   if (chars_wasted != 0)
  936.     chars_wasted = (char *) xrealloc (chars_wasted, screen_height);
  937.   else
  938.     chars_wasted = (char *) xmalloc (screen_height);
  939.   bzero (chars_wasted, screen_height);
  940.  
  941.   if (copybuf != 0)
  942.     copybuf = (char *) xrealloc (copybuf, screen_height);
  943.   else
  944.     copybuf = (char *) xmalloc (screen_height);
  945.  
  946.   if (DC_ICcost != 0)
  947.     DC_ICcost = (int *) xrealloc (DC_ICcost,
  948.                   (2 * screen_width + 1) * sizeof (int));
  949.   else
  950.     DC_ICcost = (int *) xmalloc ((2 * screen_width + 1) * sizeof (int));
  951.  
  952.   /* Always call CalcIDCosts because it allocates some vectors.
  953.      That function handles dont_calculate_costs.  */
  954.   if (s && (!TS_ins_line && !TS_del_line))
  955.     CalcIDCosts (TS_rev_scroll, TS_ins_multi_lines,
  956.          TS_fwd_scroll, TS_del_multi_lines,
  957.          s, s);
  958.   else
  959.     CalcIDCosts (TS_ins_line, TS_ins_multi_lines,
  960.          TS_del_line, TS_del_multi_lines,
  961.          0, 0);
  962.  
  963.   if (dont_calculate_costs)
  964.     {
  965.       bzero (DC_ICcost, 2 * screen_width * sizeof (int));
  966.       return;
  967.     }
  968.  
  969.   calculate_ins_del_char_costs ();
  970.  
  971.   /* Don't use TS_repeat if its padding is worse than sending the chars */
  972.   if (TS_repeat && per_line_cost (TS_repeat) * baud_rate < 9000)
  973.     RPov = string_cost (TS_repeat);
  974.   else
  975.     RPov = screen_width * 2;
  976.  
  977.   cmcostinit ();        /* set up cursor motion costs */
  978. }
  979.  
  980. term_init (terminal_type)
  981.      char *terminal_type;
  982. {
  983.   char *area;
  984.   char **address = &area;
  985.   char buffer[2044];
  986.   register char *p;
  987.   int status;
  988.  
  989.   extern char *tgetstr ();
  990.  
  991. /**
  992.  **  (sjk)++ Setup the default key bindings (Thanx to frank ridderbusch)
  993.  **/
  994. #if defined(atarist)
  995.   initialize_st_bindings();
  996. #endif 
  997.  
  998.   Wcm_clear ();
  999.   dont_calculate_costs = 0;
  1000.  
  1001.   status = tgetent (buffer, terminal_type);
  1002.   if (status < 0)
  1003.     fatal ("Cannot open termcap database file.\n");
  1004.   if (status == 0)
  1005.     fatal ("Terminal type %s is not defined.\n", terminal_type);
  1006.  
  1007. #ifdef TERMINFO
  1008.   area = (char *) malloc (2044);
  1009. #else
  1010.   area = (char *) malloc (strlen (buffer));
  1011. #endif /* not TERMINFO */
  1012.   if (area == 0)
  1013.     abort ();
  1014.  
  1015.   TS_ins_line = tgetstr ("al", address);
  1016.   TS_ins_multi_lines = tgetstr ("AL", address);
  1017.   TS_bell = tgetstr ("bl", address);
  1018.   TS_clr_to_bottom = tgetstr ("cd", address);
  1019.   TS_clr_line = tgetstr ("ce", address);
  1020.   TS_clr_screen = tgetstr ("cl", address);
  1021.   ColPosition = tgetstr ("ch", address);
  1022.   AbsPosition = tgetstr ("cm", address);
  1023.   CR = tgetstr ("cr", address);
  1024.   TS_set_scroll_region = tgetstr ("cs", address);
  1025.   TS_set_scroll_region_1 = tgetstr ("cS", address);
  1026.   RowPosition = tgetstr ("cv", address);
  1027.   TS_del_char = tgetstr ("dc", address);
  1028.   TS_del_multi_chars = tgetstr ("DC", address);
  1029.   TS_del_line = tgetstr ("dl", address);
  1030.   TS_del_multi_lines = tgetstr ("DL", address);
  1031.   TS_delete_mode = tgetstr ("dm", address);
  1032.   TS_end_delete_mode = tgetstr ("ed", address);
  1033.   TS_end_insert_mode = tgetstr ("ei", address);
  1034.   Home = tgetstr ("ho", address);
  1035.   TS_ins_char = tgetstr ("ic", address);
  1036.   TS_ins_multi_chars = tgetstr ("IC", address);
  1037.   TS_insert_mode = tgetstr ("im", address);
  1038.   TS_pad_inserted_char = tgetstr ("ip", address);
  1039.   TS_end_keypad_mode = tgetstr ("ke", address);
  1040.   TS_keypad_mode = tgetstr ("ks", address);
  1041.   LastLine = tgetstr ("ll", address);
  1042.   Right = tgetstr ("nd", address);
  1043.   Down = tgetstr ("do", address);
  1044.   if (!Down)
  1045.     Down = tgetstr ("nl", address); /* Obsolete name for "do" */
  1046. #ifdef VMS
  1047.   /* VMS puts a carriage return before each linefeed,
  1048.      so it is not safe to use linefeeds.  */
  1049.   if (Down && Down[0] == '\n' && Down[1] == '\0')
  1050.     Down = 0;
  1051. #endif /* VMS */
  1052.   if (tgetflag ("bs"))
  1053.     Left = "\b";          /* can't possibly be longer! */
  1054.   else                  /* (Actually, "bs" is obsolete...) */
  1055.     Left = tgetstr ("le", address);
  1056.   if (!Left)
  1057.     Left = tgetstr ("bc", address); /* Obsolete name for "le" */
  1058.   TS_pad_char = tgetstr ("pc", address);
  1059.   TS_repeat = tgetstr ("rp", address);
  1060.   TS_end_standout_mode = tgetstr ("se", address);
  1061.   TS_fwd_scroll = tgetstr ("sf", address);
  1062.   TS_standout_mode = tgetstr ("so", address);
  1063.   TS_rev_scroll = tgetstr ("sr", address);
  1064.   Wcm.cm_tab = tgetstr ("ta", address);
  1065.   TS_end_termcap_modes = tgetstr ("te", address);
  1066.   TS_termcap_modes = tgetstr ("ti", address);
  1067.   Up = tgetstr ("up", address);
  1068.   TS_visible_bell = tgetstr ("vb", address);
  1069.   TS_end_visual_mode = tgetstr ("ve", address);
  1070.   TS_visual_mode = tgetstr ("vs", address);
  1071.   TS_set_window = tgetstr ("wi", address);
  1072.  
  1073.   AutoWrap = tgetflag ("am");
  1074.   memory_below_screen = tgetflag ("db");
  1075.   TF_hazeltine = tgetflag ("hz");
  1076.   must_write_spaces = tgetflag ("in");
  1077.   meta_key = tgetflag ("km") || tgetflag ("MT");
  1078.   TF_insmode_motion = tgetflag ("mi");
  1079.   TF_standout_motion = tgetflag ("ms");
  1080.   TF_underscore = tgetflag ("ul");
  1081.   MagicWrap = tgetflag ("xn");
  1082.   TF_xs = tgetflag ("xs");
  1083.   TF_teleray = tgetflag ("xt");
  1084.  
  1085.   /* Get screen size from system, or else from termcap.  */
  1086.   get_screen_size (&screen_width, &screen_height);
  1087.   if (screen_width <= 0)
  1088.     screen_width = tgetnum ("co");
  1089.   if (screen_height <= 0)
  1090.     screen_height = tgetnum ("li");
  1091.  
  1092.   min_padding_speed = tgetnum ("pb");
  1093.   TN_standout_width = tgetnum ("sg");
  1094.   TabWidth = tgetnum ("tw");
  1095.  
  1096. #ifdef VMS
  1097.   /* These capabilities commonly use ^J.
  1098.      I don't know why, but sending them on VMS does not work;
  1099.      it causes following spaces to be lost, sometimes.
  1100.      For now, the simplest fix is to avoid using these capabilities ever.  */
  1101.   if (Down && Down[0] == '\n')
  1102.     Down = 0;
  1103. #endif /* VMS */
  1104.  
  1105.   if (!TS_bell)
  1106.     TS_bell = "\07";
  1107.  
  1108.   if (!TS_fwd_scroll)
  1109.     TS_fwd_scroll = Down;
  1110.  
  1111.   PC = TS_pad_char ? *TS_pad_char : 0;
  1112.  
  1113.   if (TabWidth < 0)
  1114.     TabWidth = 8;
  1115.   
  1116. /* Turned off since /etc/termcap seems to have :ta= for most terminals
  1117.    and newer termcap doc does not seem to say there is a default.
  1118.   if (!Wcm.cm_tab)
  1119.     Wcm.cm_tab = "\t";
  1120. */
  1121.  
  1122.   if (TS_standout_mode == 0)
  1123.     {
  1124.       TN_standout_width = tgetnum ("ug");
  1125.       TS_end_standout_mode = tgetstr ("ue", address);
  1126.       TS_standout_mode = tgetstr ("us", address);
  1127.     }
  1128.  
  1129.   if (TF_teleray)
  1130.     {
  1131.       Wcm.cm_tab = 0;
  1132.       /* Teleray: most programs want a space in front of TS_standout_mode,
  1133.        but Emacs can do without it (and give one extra column).  */
  1134.       TS_standout_mode = "\033RD";
  1135.       TN_standout_width = 1;
  1136.       /* But that means we cannot rely on ^M to go to column zero! */
  1137.       CR = 0;
  1138.       /* LF can't be trusted either -- can alter hpos */
  1139.       /* if move at column 0 thru a line with TS_standout_mode */
  1140.       Down = 0;
  1141.     }
  1142.  
  1143.   /* Special handling for certain terminal types known to need it */
  1144.  
  1145.   if (!strcmp (terminal_type, "supdup"))
  1146.     {
  1147.       memory_below_screen = 1;
  1148.       Wcm.cm_losewrap = 1;
  1149.     }
  1150.   if (!strncmp (terminal_type, "c10", 3)
  1151.       || !strcmp (terminal_type, "perq"))
  1152.     {
  1153.       /* Supply a makeshift :wi string.
  1154.      This string is not valid in general since it works only
  1155.      for windows starting at the upper left corner;
  1156.      but that is all Emacs uses.
  1157.  
  1158.      This string works only if the screen is using
  1159.      the top of the video memory, because addressing is memory-relative.
  1160.      So first check the :ti string to see if that is true.
  1161.  
  1162.      It would be simpler if the :wi string could go in the termcap
  1163.      entry, but it can't because it is not fully valid.
  1164.      If it were in the termcap entry, it would confuse other programs.  */
  1165.       if (!TS_set_window)
  1166.     {
  1167.       p = TS_termcap_modes;
  1168.       while (*p && strcmp (p, "\033v  "))
  1169.         p++;
  1170.       if (*p)
  1171.         TS_set_window = "\033v%C %C %C %C ";
  1172.     }
  1173.       /* Termcap entry often fails to have :in: flag */
  1174.       must_write_spaces = 1;
  1175.       /* :ti string typically fails to have \E^G! in it */
  1176.       /* This limits scope of insert-char to one line.  */
  1177.       strcpy (area, TS_termcap_modes);
  1178.       strcat (area, "\033\007!");
  1179.       TS_termcap_modes = area;
  1180.       area += strlen (area) + 1;
  1181.       p = AbsPosition;
  1182.       /* Change all %+ parameters to %C, to handle
  1183.      values above 96 correctly for the C100.  */
  1184.       while (*p)
  1185.     {
  1186.       if (p[0] == '%' && p[1] == '+')
  1187.         p[1] = 'C';
  1188.       p++;
  1189.     }
  1190.     }
  1191.  
  1192.   ScreenRows = screen_height;
  1193.   ScreenCols = screen_width;
  1194.   specified_window = screen_height;
  1195.  
  1196.   if (Wcm_init ())    /* can't do cursor motion */
  1197. #ifdef VMS
  1198.     fatal ("Terminal type \"%s\" is not powerful enough to run Emacs.\n\
  1199. It lacks the ability to position the cursor.\n\
  1200. If that is not the actual type of terminal you have, use either the\n\
  1201. DCL command `SET TERMINAL/DEVICE= ...' for DEC-compatible terminals,\n\
  1202. or `define EMACS_TERM \"terminal type\"' for non-DEC terminals.\n",
  1203.            terminal_type);
  1204. #else
  1205.     fatal ("Terminal type \"%s\" is not powerful enough to run Emacs.\n\
  1206. It lacks the ability to position the cursor.\n\
  1207. If that is not the actual type of terminal you have,\n\
  1208. use the C-shell command `setenv TERM ...' to specify the correct type.\n\
  1209. It may be necessary to do `unsetenv TERMCAP' as well.\n",
  1210.        terminal_type);
  1211. #endif
  1212.  
  1213.   delete_in_insert_mode
  1214.     = TS_delete_mode && TS_insert_mode
  1215.       && !strcmp (TS_delete_mode, TS_insert_mode);
  1216.  
  1217.   se_is_so = TS_standout_mode && TS_end_standout_mode
  1218.     && !strcmp (TS_standout_mode, TS_end_standout_mode);
  1219.  
  1220.   /* Remove width of standout marker from usable width of line */
  1221.   if (TN_standout_width > 0)
  1222.     screen_width -= TN_standout_width;
  1223.  
  1224.   UseTabs = tabs_safe_p () && TabWidth == 8;
  1225.  
  1226.   scroll_region_ok = TS_set_window || TS_set_scroll_region
  1227.     || TS_set_scroll_region_1;
  1228.  
  1229.   line_ins_del_ok = (((TS_ins_line || TS_ins_multi_lines)
  1230.               && (TS_del_line || TS_del_multi_lines))
  1231.              || (scroll_region_ok
  1232.              && TS_fwd_scroll
  1233.              && TS_rev_scroll));
  1234.  
  1235.   char_ins_del_ok = ((TS_ins_char || TS_insert_mode ||
  1236.               TS_pad_inserted_char || TS_ins_multi_chars)
  1237.              && (TS_del_char || TS_del_multi_chars));
  1238.  
  1239.   fast_clear_end_of_line = TS_clr_line != 0;
  1240.  
  1241.   init_baud_rate ();
  1242.   if (read_socket_hook)        /* Baudrate is somewhat */
  1243.                 /* meaningless in this case */
  1244.     baud_rate = 9600;
  1245. }
  1246.  
  1247. /* VARARGS 1 */
  1248. fatal (str, arg1, arg2)
  1249.      char *str;
  1250. {
  1251.   fprintf (stderr, "emacs: ");
  1252.   fprintf (stderr, str, arg1, arg2);
  1253.   fflush (stderr);
  1254.   exit (1);
  1255. }
  1256.